package com.zhang.crm.handler;

import com.zhang.crm.result.R;
import com.zhang.crm.util.JSONUtil;
import com.zhang.crm.util.ResponseUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ZhangLoginFailureHandler implements AuthenticationFailureHandler{


    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        //登录失败，返回json
        R r = R.FAIL(exception.getLocalizedMessage());
        //返回json
        String json = JSONUtil.toJSON(r);

        //写入前端页面
        ResponseUtil.write(response,json);
    }
}
